/*
 *  The MIT License
 * 
 *  Copyright 2011 ChuckToh.
 * 
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 * 
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 * 
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 */
package NicholasValueBeans;

import NicholasDatabase.SQLqueries;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ChuckToh
 */
public class Document {

    private int ownerId;
    private String category, title, content;
    private String pathname = "c:\\tmp\\";
    private int[] ids;
    private String[] titles;
    private Date dateCreated;

    public Document() {
    }

    public Document(int ownerId, String category, String title, String content, Date dateCreated) {
        this.ownerId = ownerId;
        this.category = category;
        this.title = title;
        this.content = content;
        this.dateCreated = dateCreated;
    }

    public int getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(int ownerId) {
        this.ownerId = ownerId;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Date getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }

    public int[] getIds() {
        return ids;
    }

    public String[] getTitles() {
        return titles;
    }

    public boolean insertIntoSQLTable() {
        boolean insertIntoSQLTableSuccessful = false;
        SQLqueries sql = new SQLqueries();
        try {
            int insertResults = sql.insertDocument(pathname, title, ownerId, category, dateCreated);

            if (insertResults == 1) {
                insertIntoSQLTableSuccessful = true;
            }
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        sql.closeQuery();
        return insertIntoSQLTableSuccessful;
    }

    public void writeToFile(String filename) {
        String filePathAndName = pathname + filename;
        try {
            PrintWriter pw = new PrintWriter(new FileWriter(filePathAndName));
            String HTMLContent = changeToHTMLContent(content);
            pw.write(HTMLContent);
            pw.close();
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }

    public String readFromFile(String filename) {
        String fileContents = "";
        try {
            BufferedReader br = new BufferedReader(new FileReader(pathname + filename));
            String s = "";
            while ((s = br.readLine()) != null) {
                fileContents += s;
            }
            br.close();
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
        fileContents = fileContents.replaceAll("\\n", "<br/>");
        return fileContents;
    }

    private String changeToHTMLContent(String content) {
        String HTMLContent = content;
        HTMLContent = "<h3 align='left'>" + title + "</h3>\n<p align='left'>" + HTMLContent;
        return HTMLContent;
    }

    public int[] getAllDocumentIds() {
        SQLqueries sql = new SQLqueries();
        int[] allDocumentIds = null;
        try {
            ResultSet allDocumentIdsQuery = sql.allDocumentIdsQuery();
            ResultSet allDocumentIdsQuery2 = sql.allDocumentIdsQuery();
            int allDocumentIdsCount = 0;

            while (allDocumentIdsQuery2.next()) {
                allDocumentIdsCount++;
            }
            allDocumentIdsQuery2.close();

            if (allDocumentIdsCount > 0) {
                allDocumentIds = new int[allDocumentIdsCount];
                int i = 0;
                while (allDocumentIdsQuery.next()) {
                    allDocumentIds[i] = allDocumentIdsQuery.getInt("id");
                    i++;
                }
            }
            allDocumentIdsQuery.close();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        sql.closeQuery();
        return allDocumentIds;
    }

    public String[] getAllDocumentCategories() {
        SQLqueries sql = new SQLqueries();
        String[] categories = null;
        try {
            ResultSet getDocumentCategoriesQuery = sql.getDocumentCategoriesQuery();
            ResultSet getDocumentCategoriesQuery2 = sql.getDocumentCategoriesQuery();
            int documentCategoriesCount = 0;

            while (getDocumentCategoriesQuery2.next()) {
                documentCategoriesCount++;
            }
            getDocumentCategoriesQuery2.close();

            if (documentCategoriesCount > 0) {
                categories = new String[documentCategoriesCount];
                int i = 0;

                while (getDocumentCategoriesQuery.next()) {
                    categories[i] = getDocumentCategoriesQuery.getString("category");
                    i++;
                }
            }
            getDocumentCategoriesQuery.close();

        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        sql.closeQuery();
        return categories;
    }

    public void getDocumentIdAndTitlesByCategory(String category) {
        SQLqueries sql = new SQLqueries();
        try {
            ResultSet documentIdAndTitlesByCategoryQuery = sql.getDocumentIdAndTitlesByCategoryQuery(category);
            ResultSet documentIdAndTitlesByCategoryQuery2 = sql.getDocumentIdAndTitlesByCategoryQuery(category);
            int allDocumentIdsCount = 0;

            while (documentIdAndTitlesByCategoryQuery2.next()) {
                allDocumentIdsCount++;
            }
            documentIdAndTitlesByCategoryQuery2.close();

            if (allDocumentIdsCount > 0) {
                ids = new int[allDocumentIdsCount];
                titles = new String[allDocumentIdsCount];
                int i = 0;
                while (documentIdAndTitlesByCategoryQuery.next()) {
                    ids[i] = documentIdAndTitlesByCategoryQuery.getInt("id");
                    titles[i] = documentIdAndTitlesByCategoryQuery.getString("name");
                    i++;
                }
            }
            documentIdAndTitlesByCategoryQuery.close();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        sql.closeQuery();
    }

    public boolean setDocumentDetails(int id) {
        boolean setDocumentDetailsSuccessful = false;
        SQLqueries sql = new SQLqueries();
        try {
            ResultSet getDocumentByIdQuery = sql.getDocumentByIdQuery(id);

            if (getDocumentByIdQuery.next()) {
                setOwnerId(getDocumentByIdQuery.getInt("owner"));
                setCategory(getDocumentByIdQuery.getString("category"));
                setTitle(getDocumentByIdQuery.getString("name"));
                setDateCreated(getDocumentByIdQuery.getDate("dateCreated"));
                setDocumentDetailsSuccessful = true;
            }

            getDocumentByIdQuery.close();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        sql.closeQuery();
        return setDocumentDetailsSuccessful;
    }
}
